package com.pm2.controller.meeting;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.pm2.entity.weekMeet.Meeting;
import com.pm2.entity.weekMeet.MeetingDetail;
import com.pm2.service.meeting.MeetingService;
import com.shuohe.util.returnBean.ReturnBean;

import lombok.extern.slf4j.Slf4j;

@Controller  
@RequestMapping("/pm2/meeting/*")  
@Slf4j
public class MeetingController {
	
	@Resource
	private MeetingService meetingService; 
	
	@RequestMapping(value="save.do")  
    public @ResponseBody ReturnBean save(HttpServletRequest request,HttpServletResponse response){

		Gson reGson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd")  
				.create();  
		
		String data = request.getParameter("data").replaceAll("\"\"", "null");
		String detail = request.getParameter("detail").replaceAll("\"\"", "null");
		Meeting meeting = reGson.fromJson(data, new TypeToken<Meeting>(){}.getType());
		List<MeetingDetail> meetingDetails = reGson.fromJson(detail, new TypeToken<List<MeetingDetail>>(){}.getType());

		if(data == null) return new ReturnBean(false,"会议信息不能为空");
		if(meeting.getTitle() == null) return new ReturnBean(false,"会议标题不能为空");
		if(meeting.getDate() == null) return new ReturnBean(false,"会议日期不能为空");
		
		try
		{
			return meetingService.save(meeting, meetingDetails);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	@RequestMapping(value="update.do")  
    public @ResponseBody ReturnBean update(HttpServletRequest request,HttpServletResponse response){

		Gson reGson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd")  
				.create();  
		
		String data = request.getParameter("data").replaceAll("\"\"", "null");
		String detail = request.getParameter("detail").replaceAll("\"\"", "null");
		Meeting meeting = reGson.fromJson(data, new TypeToken<Meeting>(){}.getType());
		List<MeetingDetail> meetingDetails = reGson.fromJson(detail, new TypeToken<List<MeetingDetail>>(){}.getType());

		if(data == null) return new ReturnBean(false,"会议信息不能为空");
		if(meeting.getTitle() == null) return new ReturnBean(false,"会议标题不能为空");
		if(meeting.getDate() == null) return new ReturnBean(false,"会议日期不能为空");
		
		try
		{
			return meetingService.update(meeting, meetingDetails);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	
	@RequestMapping(value="delete.do")  
    public @ResponseBody ReturnBean delete(@RequestParam("id") int id){
		
		try
		{
			return meetingService.delete(id);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	
	/**
	 * 任务评估
	 * @param master_plan_id
	 * @param finish_date
	 * @param task_response
	 * @return
	 */
	@RequestMapping(value="taskEstimate.do")  
    public @ResponseBody ReturnBean taskEstimate(@RequestParam("master_plan_id") int master_plan_id
    		,@RequestParam("finish_status") String finish_status,@RequestParam("finish_situation") String finish_situation,@RequestParam("task_response") String task_response){
		log.info("任务id=="+master_plan_id);
		log.info("完成情况=="+finish_situation);
		log.info("完成情况描述=="+task_response);
		meetingService.taskEstimate(master_plan_id,finish_status,finish_situation,task_response);
		return new ReturnBean(true,"评估成功");
	}
	
	
	@RequestMapping(value="taskFeedback.do")  
    public @ResponseBody ReturnBean taskFeedback(@RequestParam("master_plan_id") int master_plan_id
    		,@RequestParam("finish_describe") String finish_describe){
		log.info("任务id=="+master_plan_id);
		log.info("完成情况描述=="+finish_describe);
		meetingService.taskFeedback(master_plan_id,finish_describe);
		return new ReturnBean(true,"反馈成功");
	}
	
	
}
